package landlords.client.proxy;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.timeout.IdleStateHandler;
import landlords.client.handler.ProtobufTransferHandler;
import landlords.entity.ClientTransferData;
import landlords.print.SimplePrinter;

import java.util.concurrent.TimeUnit;

public class ProtobufProxy implements Proxy{
    public void connect(String serverAddress,int port) throws InterruptedException {
        EventLoopGroup group=new NioEventLoopGroup();

        Bootstrap bootstrap=new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch){
                        ch.pipeline()
                                .addLast(new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS))
                                .addLast(new ProtobufVarint32FrameDecoder())
                                .addLast(new ProtobufDecoder(ClientTransferData.ClientTransferDataProtoc.getDefaultInstance()))
                                .addLast(new ProtobufVarint32LengthFieldPrepender())
                                .addLast(new ProtobufEncoder())
                                //.addLast(new SecondProtobufCodec())
                                .addLast(new ProtobufTransferHandler());
                    }
                });

        SimplePrinter.printNotice("正在连接到："+serverAddress+":"+port);
        Channel channel=bootstrap.connect(serverAddress,port).sync().channel();
    }
}
